<template>
  <div class="update-date">
    <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="$emit('cancel')"
        @confirm="confirm"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { updateProfileInfo } from '@/api/user'
export default {
  name: 'UpdateDate',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      // 日期
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async confirm () {
      const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 静止背景点击
        duration: 0
      })
      try {
        await updateProfileInfo({
          birthday: currentDate
        })
        this.$toast.success('修改出生年月成功')
      } catch (err) {
        this.$toast.fail('修改出生年月失败')
      }
      this.$emit('cancel')
      this.$emit('input', currentDate)
    }
  }
}
</script>

<style>

</style>
